package redisdb

import (
	"github.com/monnand/goredis"
	fmt "fmt"
)

var client goredis.Client
func init() {
	client.Addr = "127.0.0.1:6379"
	client.Password = "123456"/*密码*/
}

type PassMail struct {
	M_Password	string
	M_MailAddr	string
}

func SetLoginInfo(username string, password string, mailaddr string) bool{

	//mailaddr = "1134403355@qq.com"
	//fmt.Println(username + " "+ password + " " + mailaddr)

	//tmpjson := []PassMail{PassMail{password,mailaddr}}
	//var InfoJson []byte
	//var err error
	//if InfoJson, err = json.Marshal(tmpjson); err != nil {
	//	fmt.Println(err)
	//	return false
	//}
	////strJson := fmt.Sprintf("%x", InfoJson)
	////fmt.Println(strJson)
	//
	//username = "UserLogin:" + username
	//e := client.Set(username, InfoJson)
	//if e != nil {
	//	println(username,":",e);
	//}
	//return true

	namepw := "Web:UserPw:" + username
	e := client.Set(namepw, []byte(password))
	if e != nil {
		fmt.Println(namepw,",",password,":",e)
		return false
	}
	mailuser := "Web:MAddrUser:" + mailaddr
	e = client.Set(mailuser, []byte(username))
	if e != nil {
		fmt.Println(mailuser,",",username,":",e)
		return false
	}

	return true
}

func GetLoginPassword(username string) string {
	namepw := "Web:UserPw:" + username
	pw, _ := client.Get(namepw)

	return string(pw)
}

func GetUserNameByMail(mailaddr string) string {
	mailaddr = "Web:MAddrUser:" + mailaddr
	nm, _ := client.Get(mailaddr)

	return string(nm)
}

func IsExistsMail(mailaddr string) bool {
	mailaddr = "Web:MAddrUser:" + mailaddr
	isYes, e := client.Exists(mailaddr)
	if e != nil {
		fmt.Println("验证是否存在邮箱时出错：",e)
		return false
	}
	return isYes
}

func IsExistsUserName(username string) bool {
	namepw := "Web:UserPw:" + username
	isYes, e := client.Exists(namepw)
	if e != nil {
		fmt.Println("验证是否存在用户名时出错：",e)
		return false
	}
	return isYes
}

//func ZADDUserInfo(username string, password string, emailaddr string)  {
//
//}

//func main()  {
//	SetLoginInfo("jj","lgl","578654772@qq.com")
//}